//
// Created by tacom on 22-8-31.
//

#include "MatGenerate.h"

__global__ void MatGenerate(cv::cuda::PtrStepSz<uchar3> mat) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    int j = blockIdx.y * blockDim.y + threadIdx.y;

    if (i < mat.cols && j < mat.rows){
        uchar3 data;
        data.x = i % 255;
        data.y = j % 255;
        data.z = (i + j) % 255;
        mat(j, i) = data;
    }
}

void MatGenerateWrapper(cv::cuda::GpuMat mat) {
    dim3 threadDim(32, 32);  // 一个block里面最多二维32 * 32线程
    dim3 blockDim(calFit(mat.cols, threadDim.x), calFit(mat.rows, threadDim.y));
    MatGenerate<<< blockDim, threadDim >>>(mat);
    cudaDeviceSynchronize();
}

int calFit(int x, int y) {
    return (int)((x + y - 1) / y);
}
